package com.king.wms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.king.wms.mapper.LocMapper;
import com.king.wms.mapper.SkuMapper;
import com.king.wms.pojo.Loc;
import com.king.wms.pojo.Sku;

@Service
@Transactional
public class SkuService {
	
	@Autowired
	private SkuMapper skuMapper;
	@Autowired
	private LocMapper locMapper;
	
	
	public void update(Sku sku){
		skuMapper.update(sku);
	}
	
	public void delete(Integer sku_id){
		Sku sku = skuMapper.findSkuBySku_id(sku_id);
		Loc loc = locMapper.selectByPrimaryKey(sku.getLoc_id());
		Integer quantity = sku.getQuantity();
		Integer remain = loc.getStackremianed();
		loc.setStackremianed(quantity + remain);
		locMapper.update(loc);
		skuMapper.deleteByPrimaryKey(sku_id);
	}
	
	public String insert(Sku sku){
		
		Integer quantity = sku.getQuantity();
		Integer loc_id = sku.getLoc_id();
		Loc loc = locMapper.selectByPrimaryKey(loc_id);
		Integer remain = loc.getStackremianed();
		Integer used = loc.getStackuesd();
		
		if( quantity > remain){
			return "该  "+loc.getRegion()+" 区域存放仅 "+remain+" 个单元空间存放,需要存放  "+quantity+" 个单元的商品,库存不足,请重新操作";
		}else{
			remain = remain - quantity;
			used = used + quantity;
			loc.setStackremianed(remain);
			loc.setStackuesd(used);
			skuMapper.insert(sku);
			locMapper.update(loc);
			return "success";
		}
		
		
		
	}
	
	public List<Sku> findAll(){
		//SkuExample example=new SkuExample();
		List<Sku> list = skuMapper.findAll();
		return list;
	}

	public List<Sku> findSkuByStorer_id(Integer storer_id) {
		// TODO Auto-generated method stub
		return skuMapper.findSkuByStorer_id(storer_id);
	}

	public Sku findSkuBySku_id(Integer sku_id) {
		// TODO Auto-generated method stub
		return  skuMapper.findSkuBySku_id(sku_id);
	}

	public List<Sku> findSkuByloc_id(Integer loc_id) {
		// TODO Auto-generated method stub
		return skuMapper.findSkuByloc_id(loc_id);
	}

}
